# -*- coding: UTF-8 -*-
# -*- coding: UTF-8 -*-
import pymysql
import MySQLdb
import hashlib
from config import Config
class Flag_DbUtils:
    def __init__(self):
       config_db_proxy = Config('spider.conf')
       config_db_proxy.readDb()
       self.db = pymysql.connect(host=config_db_proxy.host, user=config_db_proxy.user, passwd=config_db_proxy.passwd, db=config_db_proxy.db_data,charset="utf8")

    def insert(self, flag,did):

        sql = "insert into c_flag(flag,did) values('%s','%s')" % (flag,did)

        cursor = self.db.cursor()

        # 执行sql语句
        cursor.execute(sql)

        cursor.close()
        # 提交到数据库执行
        self.db.commit()
        # except:
        # 发生错误时回滚
        # self.db.rollback()

        # 关闭数据库连接
        # self.db.close()

    def queryFlag(self, flag):
        sql = "select * from c_flag where flag = '%s'" % (flag)
        cursor = self.db.cursor()
        cursor.execute(sql)

        # 获取所有记录列表
        results = cursor.fetchall()

        self.db.close()
        if len(results):
            return True
        else:
            return False


    def queryId(self, flag):
        sql = "select did from c_flag where flag = '%s'" % (flag)
        cursor = self.db.cursor()
        cursor.execute(sql)

        # 获取所有记录列表
        result = cursor.fetchone()

        #print sql
        #print result
        self.db.close()
        if not result is None:
            return result[0]
        else:
            return 0

    def close(self):
        self.db.close()

    def delete(self):
     '''  sql = "delete from employee where age = '%d'" % (21)
       cursor = self.db.cursor()
       cursor.execute(sql)
       self.db.commit()
       self.db.close()

       print '''

    def update(self,forward, praise, comment,flag):
        '''#content = MySQLdb.escape_string(content)
        sql = "update c_data set forward='%s', praise='%s', comment='%s' where flag = '%s'" % (forward, praise, comment,flag)
        print sql

        cursor = self.db.cursor()

        # 执行sql语句
        cursor.execute(sql)

        cursor.close()
        # 提交到数据库执行
        self.db.commit()
        # except:
        # 发生错误时回滚
        # self.db.rollback()

        # 关闭数据库连接
        # self.db.close()'''

    def queryFlag(self,flag):
       sql = "select * from c_flag where flag = '%s'" % (flag)
       #print sql
       cursor = self.db.cursor()
       cursor.execute(sql)

       # 获取所有记录列表
       results = cursor.fetchall()

       self.db.close()
       if len(results):
           return True
       else:
           return False

    def start(self):
        try:
            #DbUtils.create(self)
            #DbUtils.insert(self)
            #DbUtils.delete(self)
            #DbUtils.update(self)
            #DbUtils.query(self)
            print Flag_DbUtils.queryByFlag(self,'1')
        except Exception, e:
            print "Error: ", e

#DbUtils().start()

#m2 = hashlib.md5()
#m2.update(':【实用的家庭消防宝典，你值得拥有】三伏天已到！目前，佛山五区高温黄色预警信号均已经生')
#print m2.hexdigest()
#db= Flag_DbUtils()
#for page in range(1, 1200):
#dbutils.insert('https://weibo.cn/u/3938812452?page=1')
#print db.queryId("d3142382f958ca85b7e0564160a0fd3d")
#print db.queryFlag("a9b80b9bbe0a1211c6443c379c9bdd27")